package cn.wenzy.aurora.admin.modules.organization.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.wenzy.aurora.admin.modules.organization.bean.SysUser;
import cn.wenzy.aurora.admin.modules.organization.mapper.SysUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserService extends ServiceImpl<SysUserMapper, SysUser> {

    public IPage<SysUser> page(SysUser sysUser, IPage<SysUser> page) {
        LambdaQueryWrapper<SysUser> wrapper = lambdaQueryWrapper(sysUser);
        return page(page, wrapper);
    }

    public List<SysUser> list(SysUser sysUser) {
        LambdaQueryWrapper<SysUser> wrapper = lambdaQueryWrapper(sysUser);
        return list(wrapper);
    }

    private LambdaQueryWrapper<SysUser> lambdaQueryWrapper(SysUser sysUser) {
        LambdaQueryWrapper<SysUser> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(sysUser.getUserCode()), SysUser::getUserCode, sysUser.getUserCode());
        wrapper.like(StrUtil.isNotBlank(sysUser.getUserName()), SysUser::getUserName, sysUser.getUserName());
        wrapper.eq(StrUtil.isNotBlank(sysUser.getPhoneNo()), SysUser::getPhoneNo, sysUser.getPhoneNo());
        wrapper.eq(StrUtil.isNotBlank(sysUser.getEmail()), SysUser::getEmail, sysUser.getEmail());
        wrapper.eq(StrUtil.isNotBlank(sysUser.getStatus()), SysUser::getStatus, sysUser.getStatus());
        wrapper.eq(ObjectUtil.isNotNull(sysUser.getSex()), SysUser::getSex, sysUser.getSex());
        wrapper.orderByAsc(SysUser::getSortNo, SysUser::getUserCode);
        return wrapper;
    }
}
